package com.softwinner.bt.Fragment;

import java.util.ArrayList;

import com.softwinner.bt.BtApplication;
import com.softwinner.bt.CallDialog;
import com.softwinner.bt.MainActivity;
import com.softwinner.bt.R;
import com.softwinner.bt.adapter.CalllogAdapter;
import com.softwinner.bt.bean.Record;
import com.softwinner.bt.policy.BTManager;
import com.softwinner.bt.util.BTLog;
import com.softwinner.bt.util.Command;
import com.softwinner.bt.util.DatabaseHelper;
import com.softwinner.bt.util.Utils;

import android.app.Fragment;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CalllogFragment  extends BaseFragment implements OnClickListener {
		
	private Button mDialer;
	private Button mBack;
	private Button mDel;
	private ListView mList;
    private ArrayList<Record> records = new ArrayList<Record>();
    private CalllogAdapter mAdapter;
    private DatabaseHelper mDatabaseHelper;
    private BTManager mBTManager;
    
    private BroadcastReceiver receiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			BTLog.i(CalllogFragment.class, "action = " + action);
			updateRecord();
		}
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.bt_list_fragment, container, false);
		initView(view);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mBTManager = BTManager.getInstance();
		mDatabaseHelper = DatabaseHelper.getDatabaseHelper(mActivity);
        records = new ArrayList<Record>();
        records = mDatabaseHelper.queryAllRecords();
        mAdapter = new CalllogAdapter(mActivity, records);
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(mItemClickListener);
		
        IntentFilter filter = new IntentFilter();
        filter.addAction(Utils.ACTION_UPDATE_RECORDS);
        filter.addAction(Utils.ACTION_BTSTATE_CHANGED);
        getActivity().registerReceiver(receiver, filter);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		getActivity().unregisterReceiver(receiver);
		super.onDestroy();
	}

	private void initView(View view){
		view.findViewById(R.id.list_f5).setVisibility(View.GONE);
		mDialer = (Button)view.findViewById(R.id.list_dialer);
		mBack = (Button)view.findViewById(R.id.list_back);
		mDel = (Button)view.findViewById(R.id.list_del);		
		mList = (ListView)view.findViewById(R.id.bt_list);
		
		mDialer.setOnClickListener(this);
		mBack.setOnClickListener(this);
		mDel.setOnClickListener(this);
	}
	
	public void updateRecord(){
		records = mDatabaseHelper.queryAllRecords();
		mAdapter.setDataChange(records);
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
        case R.id.list_dialer:
            if (records.size() > 0) {
            	if(mActivity.getState() != BTManager.STATE_CONNECTED){
            		Toast.makeText(mActivity, R.string.msg_unconnectedorcalling, 1000).show();
            		return;
            	}
                Record record = records.get(mCurrentPosition);
                mBTManager.sendCommand(Command.SEND_MAKECALL + record.getNumber());
            }
            break;
        case R.id.list_back:
            if (mCurrentPosition >= 0 && records.size() > 0&& mCurrentPosition<records.size()) {
                mDatabaseHelper.deleRecord(records.get(mCurrentPosition));
                //records = mDatabaseHelper.queryAllRecords();
                //mAdapter.setDataChange(records);
                mCurrentPosition=-1;
                updateRecord();
            }
            break;
        case R.id.list_del:
            if (records.size() > 0) {
            	mDatabaseHelper.delAllRecord();
//                records = mDatabaseHelper.queryAllRecords();
//                mAdapter.setDataChange(records);
                mCurrentPosition=-1;
                updateRecord();
            }
            break;
        default:
            break;
        }
	}

	private int mCurrentPosition = 0;
    private OnItemClickListener mItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            mCurrentPosition = position;
            mAdapter.setSelected(mCurrentPosition);
            mAdapter.notifyDataSetChanged();
        }
    };
}
